/**
 * @param {Egg.Application} app - egg application
 */
module.exports = app => {
  const { router, controller } = app;
  router.get('/', controller.home.index);
  router.post("/user/reg",controller.user.reg)
  router.post("/user/login",controller.user.login)
  router.post("/user/logout",controller.user.logout)
  router.post("/search/user",controller.search.user)
  router.post("/user/addFriend",controller.apply.addFriend)
  router.post("/apply/list/:page",controller.apply.list);
  router.post("/apply/:id/handle",controller.apply.handle);
  router.post("/friend/list",controller.friend.list);
  router.post("/friend/read/:id",controller.friend.read);
  router.post("/friend/setblack/:id",controller.friend.setblack);
  router.post("/friend/setstar/:id",controller.friend.setstar);
  router.post("/friend/setmomentauth/:id",controller.friend.setmomentauth);
  router.post("/report/save",controller.report.save);
  router.post("/friend/setrearktag/:id",controller.friend.setrearktag);
  router.post("/taglist",controller.tag.tagslist);
  router.post("/send",controller.chat.send);
  router.post("/group/create",controller.group.create);
  router.post("/group/getlxmessage",controller.chat.getlxmessage);
  router.post("/group/list",controller.group.list);
  router.post("/group/info",controller.group.info);
  router.post("/group/grouprename",controller.group.grouprename);
  router.post("/group/groupupdateremark",controller.group.groupupdateremark);
  router.post("/group/groupupdatenickname",controller.group.groupupdatenickname);
  router.post("/group/quit",controller.group.quit);
  router.get("/group/qrcode/:id",controller.group.qrcode);
  router.get("/user/qrcode/:id",controller.user.qrcode);
  router.post("/upload",controller.common.upload);


  // app/router.js
app.ws.use(async (ctx, next) => {
  // 获取参数 ws://localhost:7001/ws?token=123456
  // ctx.query.token
  // 验证用户token
  let user = {};
  console.log(ctx.header.token,"tokennn")
  let token = ctx.query.token;
  try {
    user = ctx.checktoken(token);
    // 验证用户状态
    let userCheck = await app.model.User.findByPk(user.id);
    if (!userCheck) {
      ctx.websocket.send(JSON.stringify({
        msg: "fail",
        data: "用户不存在",
      }));
      return ctx.websocket.close();
    }
    if (!userCheck.status) {
      ctx.websocket.send(JSON.stringify({
        msg: "fail",
        data: "你已被禁用",
      }));
      return ctx.websocket.close();
    }
    // 用户上线
    app.ws.user = app.ws.user ? app.ws.user : {};
    // 下线其他设备
    if (app.ws.user[user.id]) {
      app.ws.user[user.id].send(JSON.stringify({
        msg: "fail",
        data: "你的账号在其他设备登录",
      }));
      app.ws.user[user.id].close();
    }
    // 记录当前用户id
    ctx.websocket.user_id = user.id;
    app.ws.user[user.id] = ctx.websocket;
    await next();
  } catch (err) {
    console.log(err);
    let fail = err.name === "TokenExpiredError"
      ? "token 已过期! 请重新获取令牌"
      : "Token 令牌不合法!";
    ctx.websocket.send(JSON.stringify({
      msg: "fail",
      data: fail,
    }));
    // 关闭连接
    ctx.websocket.close();
  }
});

// 路由配置
app.ws.route("/ws", controller.chat.connect);
  // //ws

  // app.ws.route("/ws",controller.chat.connect)
};
